﻿using God.ShoppingWebApi.Entities;
using God.ShoppingWebApi.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Application.Services;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using God.ShoppingWebApi.DTO.Comm;
using God.ShoppingWebApi.DTO.User;

namespace God.ShoppingWebApi.Service
{
    public class AdminCommodTypeService : ApplicationService, IAdminCommodTypeService
    {
        IRepository<CommodType, Guid> _repository;
        public AdminCommodTypeService(IRepository<CommodType, Guid> repository)
        {
            _repository = repository;
        }
        [HttpPost,Route("api/add")]
        /// <summary>
        /// 商品添加
        /// </summary>
        /// <param name="commodInfo"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task AddAsync(CommodTypeDTO commodInfo)
        {
            CommodType ci = new CommodType(GuidGenerator.Create(),commodInfo.CommodTypeId, commodInfo.CommodTypeParentId, commodInfo.CommodTypeName, commodInfo.CommodTypeDisplay, CurrentUser.Id);

            await _repository.InsertAsync(ci);
        }
        [Route("api/del")]
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="gu"></param>
        /// <returns></returns>
        public async Task DeleteAsync(Guid gu)
        {
            await _repository.DeleteAsync(gu);
        }
        [Route("api/fan")]
        /// <summary>
        /// 返填
        /// </summary>
        /// <param name="gid"></param>
        /// <returns></returns>
        public async Task<CommodTypeDTO> Edit(Guid gid)
        {
            var data = await _repository.GetAsync(gid);
            var book = ObjectMapper.Map<CommodType, CommodTypeDTO>(data);
            return book;
        }
        [Route("api/show")]
        /// <summary>
        /// 分类显示
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<CommodTypeDTO>> GetListAsync()
        {
            var str = await _repository.GetListAsync();

            return ObjectMapper.Map<List<CommodType>, List<CommodTypeDTO>>(str);
        }
        [Route("api/upt")]
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="commodTypeDTO"></param>
        /// <returns></returns>
        public async Task<CommodTypeDTO> Update(CommodTypeDTO commodTypeDTO)
        {
            var data = await _repository.GetAsync(commodTypeDTO.Id);
            if (data != null)
            {
                data.LastModificationTime = DateTime.Now;
                data.CommodTypeId = commodTypeDTO.CommodTypeId;
                data.CommodTypeParentId = commodTypeDTO.CommodTypeParentId;
                data.CommodTypeName = commodTypeDTO.CommodTypeName;
                data.CommodTypeDisplay = commodTypeDTO.CommodTypeDisplay;
                var info= await _repository.UpdateAsync(data);
                return commodTypeDTO;

            }
            else
            {
                return null;
            }
        }

        public Task<UserLoginDTO> ValidateCode()
        {
            throw new NotImplementedException();
        }





        //  //验证码
        //  public async Task<UserLoginDTO> ValidateCode()
        //  {
        //      ValidateCodeHelper validateCodeHelper = new ValidateCodeHelper();//拖的
        //
        //      string code = "";
        //      MemoryStream ms = validateCodeHelper.Create(out code);
        //
        //      RedisHelper.Set("LoginValidateCode", code, 300);
        //      return ms.ToArray();
        //  }



        //  [HttpPost]//验证码
        //  public IActionResult Yan([FromForm] DTO dTO)
        //  {
        //      string code = RedisHelper.Get("LoginValidateCode");
        //      var list = JsonConvert.DeserializeObject<Users>(JsonConvert.SerializeObject(dTO));
        //      if (string.IsNullOrEmpty(code))
        //      {
        //          return Ok(new { result = "图片验证过期", code = -2 });
        //      }
        //      if (code.ToLower() == dTO.ReYan.ToLower())
        //      {
        //          return Ok(new { result = "验证成功", code = 1 });
        //      }
        //      else
        //      {
        //          return Ok(new { result = "验证失败", code = 0 });
        //      }
        //  }
    }
}
